package jdec.jdt.ls.decompiler.jadx;

import static org.eclipse.jdt.ls.core.internal.handlers.MapFlattener.getValue;
import java.io.File;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
import jdec.jdt.ls.decompiler.common.CachingDecompiler;
import jdec.jdt.ls.decompiler.common.JavaDecompilerPlugin;
import jdec.jdt.ls.decompiler.common.SupportDecompilerType;
import jdec.jdt.ls.decompiler.common.util.FileUtil;
import jdec.jdt.ls.decompiler.common.util.JarClassExtractor;

public class JadxDecompiler2 extends CachingDecompiler {

    public static final String OPTIONS_KEY = "java.decompiler.jadx";
    public static final String DECOMPILED_HEADER =
            " // Source code was generated by the JADX decompiler.\n";

    Map<String, Object> options = new ConcurrentHashMap<>();

    @Override
    public void setDecompilePreferences(Preferences preferences) {

        Object overrides = getValue(preferences.asMap(), OPTIONS_KEY);
        if (overrides instanceof Map) {
            @SuppressWarnings("unchecked")
            Map<String, Object> overridesMap = (Map<String, Object>) overrides;
            for (String key : overridesMap.keySet()) {
                Object value = overridesMap.get(key);
                if (value instanceof Boolean) {
                    value = ((Boolean) value).booleanValue() ? "1" : "0";
                }
                options.put(key, value.toString());
            }
        }

        // debugger
        // options.put(IFernflowerPreferences.DUMP_ORIGINAL_LINES, "1"); //$NON-NLS-1$
        // options.put(IFernflowerPreferences.BYTECODE_SOURCE_MAPPING, "1"); //$NON-NLS-1$
    }

    @Override
    protected String decompileContent(URI uri, IProgressMonitor monitor) throws CoreException {
        return getContent(new BytecodeProvider(uri), monitor);
    }

    @Override
    protected String decompileContent(IType type, IProgressMonitor monitor) throws CoreException {
        IClassFile classFile = type.getClassFile();

        return getContent(new BytecodeProvider(classFile), monitor);
    }

    private String getContent(BytecodeProvider provider, IProgressMonitor monitor) {
        DummyLogger logger = new DummyLogger();


        String code = DECOMPILED_HEADER + null;
        // return new EclipseFormater().formatSource(code);
        return code;
    }


    @Override
    protected String decompileFromArchive(String archivePath, String packege, String className,
            IProgressMonitor monitor) throws CoreException {
        File workingDir = new File(JavaDecompilerPlugin.tempDir());

        try {
            workingDir.mkdirs();
            JarClassExtractor.extract(archivePath, packege, className, true,
                    workingDir.getAbsolutePath());
            return null;
        } catch (Exception e) {
            throw new CoreException(new Status(IStatus.ERROR, "java.decompiler.jadx",
                    "Error decompiling", e));
        } finally {
            FileUtil.deltree(workingDir);
        }
    }

    @Override
    protected String getDecompilerType() {
        return SupportDecompilerType.JADX;
    }

}
